<?php defined('SYSPATH') or die('No direct script access.');

// -- Environment setup --------------------------------------------------------

// Load the core Kohana class
require SYSPATH.'classes/kohana/core'.EXT;

if (is_file(APPPATH.'classes/kohana'.EXT))
{
    // Application extends the core
    require APPPATH.'classes/kohana'.EXT;
}
else
{
    // Load empty core extension
    require SYSPATH.'classes/kohana'.EXT;
}

if ( ! ini_get('short_open_tag'))
{
    trigger_error('请修改 php.ini 文件并将 "short_open_tag" 设置为 "on"', E_USER_ERROR);
}

/**
 * Set the default time zone.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/timezones
 */
date_default_timezone_set('Asia/Shanghai');

/**
 * Set the default locale.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/function.setlocale
 */
setlocale(LC_ALL, 'zh_cn.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @link http://kohanaframework.org/guide/using.autoloading
 * @link http://www.php.net/manual/function.spl-autoload-register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @link http://www.php.net/manual/function.spl-autoload-call
 * @link http://www.php.net/manual/var.configuration#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('zh-cn');

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
if (isset($_SERVER['KOHANA_ENV']))
{
    Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - integer  cache_life  lifetime, in seconds, of items cached              60
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 * - boolean  expose      set the X-Powered-By header                        FALSE
 */
Kohana::init(array(
    'base_url'   => NULL,
    'index_file' => '/',
    'profile'    => TRUE,
    'errors'     => TRUE,
    'caching'    => FALSE,
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH.'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
    'cache'      => MODPATH.'cache',      // Caching with multiple backends
    'captcha'    => MODPATH.'captcha',    // Captcha of results
    'database'   => MODPATH.'database',   // Database access
    'pagination' => MODPATH.'pagination', // Paging of results
));

/**
 * Set the default database instance
 */
Database::$default = 'default';

/**
 * Set the default cache driver
 */
Cache::$default = 'file';

/**
 * Set the default session adapter
 */
Session::$default = 'database';

/**
 * Set the cookie salt
 */
Cookie::$salt = 'xafhqw%#%$#(3';

/**
 * Set the default cookie domain
 */
// Cookie::$domain = '.example.com';

/**
 * Attach a database config reader to DB.
 */
Kohana::$config->attach(new Config_Database_Writer);

/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))<url_suffix>', array('url_suffix'=>'(\.html)?'))
    ->defaults(array(
        'directory'  => 'admin',
        'controller' => 'welcome',
        'action'     => 'index',
    ));
Route::set('item', 'item/detail(/<id>)<url_suffix>', array('url_suffix' => '(\.html)?'))
    ->defaults(array(
        'controller' => 'item',
        'action'     => 'detail',
    ));
Route::set('item', 'item(/<category>)<url_suffix>', array('url_suffix' => '(\.html)?'))
    ->defaults(array(
        'controller' => 'item',
        'action'     => 'list',
    ));
Route::set('default', '(<controller>(/<action>(/<id>)))<url_suffix>', array('url_suffix' => '(\.html)?'))
    ->defaults(array(
        'controller' => 'welcome',
        'action'     => 'index',
    ));